package com.tanhua.admin.module.dynamicmanage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.admin.module.dynamicmanage.service.ManagerVideoService;
import com.tanhua.admin.module.dynamicmanage.vo.PageResult;
import com.tanhua.admin.module.dynamicmanage.vo.VideoVo;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.pojo.VideoInfo;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 视频翻页
 */
@Service
@Slf4j
public class ManagerVideoServiceImpl implements ManagerVideoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private VideoInfoServiceImpl videoInfoService;

    @Reference(version = "1.0.0")
    private VideoApi videoApi;

    @Override
    public PageResult queryVideos(Map<String, Object> param) {
        if (ObjectUtil.isEmpty(param)){
            return null;
        }
        //pagesize和pages作为分页参数
        Integer pagesize = Integer.valueOf(param.get("pagesize").toString());
        Integer page = Integer.valueOf(param.get("page").toString());
        String sortProp = param.get("sortProp").toString();
        String sortOrder = param.get("sortOrder").toString();
        Long uid = Long.valueOf(param.get("uid").toString());

        Map<String, Object> videoInfoMap = videoInfoService.queryVideoInfoList(pagesize, page, sortProp, sortOrder,uid);

        IPage<VideoInfo> videoInfoPage = (IPage<VideoInfo>) videoInfoMap.get("videoInfoPage");
        
        //得到视频信息集合
        List<VideoInfo> videoInfoList = videoInfoPage.getRecords();
        
        //用户信息
        QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
        userInfoQueryWrapper.select("*");

        List<UserInfo> userInfoList = userInfoMapper.selectList(userInfoQueryWrapper);
        if (ObjectUtil.isEmpty(userInfoList)){
            return null;
        }

        //查询视频信息表
        List<com.tanhua.dubbo.server.pojo.Video> videoList = videoApi.queryVideo();
        if (ObjectUtil.isEmpty(videoList)) {
            return null;
        }

        List<VideoVo> videoVos = new ArrayList<>();

        for (VideoInfo videoInfo : videoInfoList) {
            VideoVo videoVo = new VideoVo();
            videoVo.setId(videoInfo.getId());
            videoVo.setUserId(videoInfo.getUserId());
            videoVo.setCreateDate(videoInfo.getCreateDate());
            videoVo.setReportCount(videoInfo.getReportCount());
            videoVo.setLikeCount(videoInfo.getLikeCount());
            videoVo.setCommentCount(videoInfo.getCommentCount());
            videoVo.setForwardingCount(videoInfo.getForwardingCount());
            videoVos.add(videoVo);
        }

        for (VideoVo videoVo : videoVos) {
            for (UserInfo userInfo : userInfoList) {
                    videoVo.setNickname(userInfo.getNickName());
            }
            for (Video video : videoList) {
                    videoVo.setVideoUrl(video.getVideoUrl());
                    videoVo.setPicUrl(video.getPicUrl());
            }
        }

        //填充返回数据
        PageResult pageResult = new PageResult();
        pageResult.setPagesize(pagesize);
        pageResult.setPage(page);

        pageResult.setItems(videoVos);

        Integer count = (Integer) videoInfoMap.get("count");
        pageResult.setCounts(count);

        return pageResult;
    }
}